import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Question19 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        
        int t = in.nextInt();

        for (int i = 0; i < t; i++) {
            int n = in.nextInt();
            int k = in.nextInt();
            int a1 = in.nextInt() - 1;
            int a2 = in.nextInt() - 1;
            int a3 = in.nextInt() - 1;

            int[] temp = {a1, a2, a3};

            Arrays.sort(temp);
            a1 = temp[0];
            a2 = temp[1];
            a3 = temp[2];

            if (3 * k > n) {
                System.out.println(-1);
                continue;
            }

            int ret = 0;

            int bw12 = a2 - a1;
            int bw23 = a3 - a2;
            int bw13 = a3 - a1;
            int bw31 = n - a3 + a1;

            int bw = Math.min(bw13, bw31);

            temp[0] = bw12;
            temp[1] = bw23;
            temp[2] = bw;

            Arrays.sort(temp);
            // System.out.println(Arrays.toString(temp));

            if (temp[0] < k) {
                ret += k - temp[0];
            }

            if (temp[1] < k) {
                ret += k - temp[1];
            }
            

            System.out.println(ret);
        }
    }
}